<%--
    Document   : adminCategory
    Created on : Apr 7, 2011, 2:04:48 PM
    Author     : Robert Gunawan
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="jesstalk.Db"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<script type="text/javascript" src="script/ajaxwrap.js"></script>
<script type= "text/javascript">
    aj = new AjaxWrap(); //bikin aj sebagai AjaxWrap
</script>

<%
if((session.getAttribute("username") == null)||(!session.getAttribute("username").equals("admin"))) //ilegal access
        {
            response.sendRedirect("index.jsp");
        }
Db Admin = Db.get(getServletContext());
ArrayList<String> ListUsername = new ArrayList<String>();

//==================================AREA LOG OUT============================================
if(request.getParameter("Logout") != null)
    {
    //session.removeAttribute("kategoriBefore");
    //session.removeAttribute("kategoriAfter");
    session.removeAttribute("username");
    response.sendRedirect("index.jsp");
    }

//==================================BAGIAN CEK SESSION BEFORE AFTER ==========================
if(request.getParameter("Back")!= null)
    {
    session.setAttribute("kategoriAfter", session.getAttribute("kategoriBefore"));
    String Query ="select * from kategori where id='"+session.getAttribute("kategoriBefore")+"'";
    ArrayList<String> Before = Admin.getQuery("jesstalk", Query, "idParent");
    if(Before.size() == 0)
        {
        session.setAttribute("kategoriBefore", "0");
        }
    else
        {
        session.setAttribute("kategoriBefore", Before.get(0));
        }
    }
if(request.getParameter("Go")!= null)
    {
    session.setAttribute("kategoriBefore", session.getAttribute("kategoriAfter"));
    session.setAttribute("kategoriAfter", request.getParameter("Go"));
    }

//==============================BAGIAN DELETE MODERATOR==========================================
if(request.getParameter("DelMod") != null) //ada perintah delete
    {
    ArrayList<String>ModDel = new ArrayList<String>();
    ModDel.add(request.getParameter("Category"));
    int nModDel = ModDel.size();
    int its;
    for(its =0;its<nModDel;its++)
        {
        String zzz = "select * from kategori where idParent='"+ModDel.get(its)+"'";
        ArrayList<String>meong = Admin.getQuery("jesstalk", zzz, "id");
        ModDel.addAll(meong);
        //delete moderasi
        String DelMod = "delete from moderasi where (idUser='" + request.getParameter("ModId")+"' and idKategori ='"+ModDel.get(its)+"')";
        Admin.deleteFrom("jesstalk", DelMod);
        nModDel = ModDel.size();
        }
    }

//==============================BAGIAN ADD MODERATOR============================================
if(request.getParameter("ModUsrName")!= null) //ada mod yang akan ditambah
    {
    String QueryMod = "select * from user where username ='"+request.getParameter("ModUsrName")+"'";
    ArrayList<String> test = Admin.getQuery("jesstalk", QueryMod, "id");
    if(test.size()!=1)
        {
        %>
        <script type="text/javascript">
            alert("No Username found!");
        </script>
        <%
        }
    else
        {
        ArrayList<String>ModCat = new ArrayList<String>();

        ModCat.add(session.getAttribute("kategoriAfter").toString());
        int nModDel = ModCat.size();
        int its;
        for(its =0;its<nModDel;its++)
            {
            String zzz = "select * from kategori where idParent='"+ModCat.get(its)+"'";
            ArrayList<String>meong = Admin.getQuery("jesstalk", zzz, "id");
            ModCat.addAll(meong);
            //insert moderasi
            String InsertMod = "insert into moderasi (idUser,idKategori) values('"+test.get(0)+"','"+ModCat.get(its)+"')";
            Admin.updateFrom("jesstalk", InsertMod);
            %>
            <script type="text/javascript">
                aj.openAndSend("GET", "ChatServlet?setuserasmod=1&username="+<%out.print("\""+request.getParameter("ModUsrName")+"\"");%>+"&categId="+<%out.print(ModCat.get(its));%>, true, null);
            </script>
            <%
            nModDel = ModCat.size();
            }
        }
    }

//================================BAGIAN DELETE CATEGORY=============================================
if(request.getParameter("DelCat")!=null)
    {
    //hapus kategori
    ArrayList<String> tempDel = new ArrayList<String>();
    tempDel.add(request.getParameter("DelCat"));
    int nTempDel = tempDel.size();
    int it;
    //mulai hapus
    for(it=0;it<nTempDel;it++)
        {
        //query dulu mana yang masuk anak dari elemen ke it
        String aaa = "select * from kategori where idParent ='"+tempDel.get(it)+"'";
        ArrayList<String> bbb = Admin.getQuery("jesstalk", aaa, "id");
        //delete kategori 1-1
        String DeleteCat = "delete from kategori where id='"+tempDel.get(it)+"'";
        Admin.deleteFrom("jesstalk", DeleteCat);
        //hapus moderasi 1-1
        String Category = "delete from moderasi where idKategori='"+tempDel.get(it)+"'";
        Admin.deleteFrom("jesstalk", Category);
        tempDel.addAll(bbb);
        nTempDel = tempDel.size();
        }
    %>
            <script type="text/javascript">
                aj.openAndSend("GET", "ChatServlet?delcategoryfromsystem=1", true, null);
            </script>
    <%
    }
//===============================BAGIAN RENAME CATEGORY==========================================
if(request.getParameter("NamaRename")!= null)
    {
    String Update = "update kategori set nama ='"+request.getParameter("NamaRename")+"' where nama ='"+request.getParameter("NamaAsli")+"'";
    Admin.updateFrom("jesstalk", Update);
    }

//==================================BAGIAN NEW CATEGORY=========================================
if(request.getParameter("NewCat")!= null)
    {
    String InsertCat = "insert into kategori(nama,idParent) values('"+request.getParameter("NewCat")+"','"+session.getAttribute("kategoriAfter")+"')";
    Admin.insertInto("jesstalk", InsertCat);
    //tambah moderator di atasnya persis
    String addMod = "select * from moderasi where idKategori='"+session.getAttribute("kategoriAfter")+"'";
    ArrayList<String> addModArray = Admin.getQuery("jesstalk", addMod, "idUser");
    String temps;
    String queryCat = "select * from kategori where (nama='"+request.getParameter("NewCat")+"' and idParent='"+session.getAttribute("kategoriAfter")+"')";
    ArrayList<String> idPrimary = Admin.getQuery("jesstalk", queryCat, "id");
    for(int zzz = 0;zzz<addModArray.size();zzz++)
        {
        temps = "insert into moderasi (idUser,idKategori) values('"+addModArray.get(zzz)+"','"+idPrimary.get(0)+"')";
        Admin.insertInto("jesstalk", temps);
        }
    }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Admin Category Controller Page</title>
        <link href="style/styleAdmin.css" rel="stylesheet" type="text/css">
    </head>
    <body>
        <div id="DivLogout">
            <a href="adminCategory.jsp?Logout=1">Log Out</a>
        </div>
        <div id="MenuAdmin">
            List Option :
            <a href="adminUserControl.jsp">User Setting</a>
        </div>

                <%
                String Kategori = "select * from kategori where id ='"+ session.getAttribute("kategoriAfter")+"'";
                ArrayList<String> Parent = Admin.getQuery("jesstalk", Kategori, "nama");
                String alala;
                if(Parent.size()==0)
                    {
                    alala = "root";
                    }
                else
                    {
                    alala = Parent.get(0);
                    }
                %>
        <div id ="judulAdmin">
        <h1>Admin Category Controller Page of <% out.print(alala); %></h1>
        </div>
        <div id="pathCategory">
            <!-- pathCategory!-->
            <%
            ArrayList<String> pathList = new ArrayList<String>();
            ArrayList<String> pathName = new ArrayList<String>();
            if(Parent.size()!=0)
                {
                //inisiasi nilai awal array
                pathList.add(session.getAttribute("kategoriAfter").toString());
                pathName.add(Parent.get(0));
                int nPathList = pathList.size();
                int itp;
                //memulai pencarian
                for (itp = 0;itp< nPathList;itp++)
                    {
                        String kodok = "select * from kategori where id='"+pathList.get(itp)+"'";
                        ArrayList<String> test = Admin.getQuery("jesstalk", kodok, "idParent");
                        ArrayList<String> test2 = Admin.getQuery("jesstalk", kodok, "nama");
                        if(test.size()!=0)
                            {
                            pathList.add(test.get(0));
                            pathName.add(test2.get(0));
                            }

                        //tambah jumlah
                        nPathList = pathList.size();
                    }  
                }
                out.print("<b>Path: root");
                for(int aaa=pathName.size()-1;aaa>0;aaa--)
                    {
                    out.print("/"+pathName.get(aaa));
                    }
                out.print("</b>");
            %>
        </div>
        <br/>
        <div id="content">
            <div id="categoryContent">
                <%
                
                if(Parent.size()==0)
                    {
                    out.print("Category List of <b>root</b> <br />");
                    }
                else
                    {
                %>
                    Category List of<%out.print(" <b>"+Parent.get(0)+"</b>");%>
                    <br />
                <%
                    }
                %>
                <table id="table1">
                    <div id="trBorder">
                        <tr class="bdr">
                            <td class="bdr"><center>Name</center></td>
                            <td class="bdr"><center>Delete?</center></td>
                            <td class="bdr"><center>Rename?</center></td>
                        </tr>
                    </div>
                <%
                if(!session.getAttribute("kategoriAfter").equals("0")) // jika sekarang bukan root
                    {
                    //tampilkan back folder
                    out.print("<tr><td><a href='adminCategory.jsp?Back="+session.getAttribute("kategoriBefore")+"'>../</a></td><td></td><td></td></tr>");
                    }
                String Query = "select * from kategori where idParent='"+session.getAttribute("kategoriAfter")+"'";
                //out.println(Query);
                //dapat id dan nama kategori dari session after
                ArrayList<String>id = Admin.getQuery("jesstalk", Query, "id");
                ArrayList<String>name = Admin.getQuery("jesstalk", Query, "nama");
                //print seluruh kategori yang ada di dalam kategori after
                //out.println(id.size());
                int i;
                for(i=0;i<id.size();i++)
                    {
                    %>
                    <div id="trBorder">
                    <tr class="bdr">
                        <td class="bdr">
                            <!-- Bagian Nama!-->
                        <%
                        out.print("<a href='adminCategory.jsp?Go="+id.get(i)+"'>"+name.get(i)+"</a>");
                        %>
                        </td>
                        <td class="bdr">
                            <!-- Bagian Delete!-->
                            <%
                            out.print("<a href ='adminCategory.jsp?DelCat="+id.get(i)+"'>Delete</a>");
                            %>
                        </td>
                        <td class="bdr">
                            <!-- Bagian Rename!-->
                                <form id="Rename" name="Rename" method="post" action="#">
                                    <input type="hidden" value ="<% out.print(name.get(i));%>" id="NamaAsli" name="NamaAsli" />
                                    <input type="text" value="<%out.print(name.get(i)); %>" id="NamaRename" name="NamaRename" />
                                    <input type="submit" id="RenameSubmit" name="SenameSubmit" />
                                </form>
                        </td>
                    </tr>
                    </div>
                    <%
                    }
                %>
                    </table>
                    <br />
                    Add Category
                    <br />
                    <form id="InsertCat" name="InsertCat" action="#" method="post">
                        Name Category: <input type="text" id="NewCat" name="NewCat" />
                        <input type="submit" id="SubmitCat" name="SubmitCat" />
                    </form>
            </div>
            <div id="border1">
            </div>
            <div id="moderatorContent">
                Moderator List
                <br />
                <%
                Query = "select * from moderasi where idKategori = '"+session.getAttribute("kategoriAfter")+"'";
                ArrayList<String>Moderator = Admin.getQuery("jesstalk", Query, "idUser");
                if((Moderator.size()==0))
                    {
                    out.print("No moderator set <br />");
                    }
                else
                    {
                    %>
                    <table id="table2">
                        <tr class="bdr">
                            <td class="bdr"><center>Name</center></td>
                            <td class="bdr"><center>Unset?</center></td>
                        </tr>
                    <%
                        for(i=0;i<Moderator.size();i++)
                            {
                            String QueryNama = "select * from user where id='"+Moderator.get(i)+"'";
                            ArrayList<String>UsrMod = Admin.getQuery("jesstalk", QueryNama, "username");
                            %>
                            <tr class="bdr">
                                <td class="bdr">
                            <%
                                out.print(UsrMod.get(0));
                            %>
                                </td>
                                <td class="bdr">
                            <%
                                out.print("<a href='adminCategory.jsp?DelMod=1&ModId="+Moderator.get(i) +"&Category="+session.getAttribute("kategoriAfter")+"'>Unset</a>");
                            %>
                                </td>
                            </tr>
                            <%
                            }
                             %>
                    </table>
               <%
                  }
                %>
                <br />
                Add Moderator
                <br />
                <form id="InsertMod" name="InsertMod" action="#" method="post">
                    Username: <input type="text" id="ModUsrName" name="ModUsrName" />
                    <input type="submit" id="SubmitMod" name="SubmitMid" />
                </form>
            </div>
            <div id="tesAjax">
                
            </div>

            </div>
    </body>
</html>
